<?php
// 只能在当前 request 生命周期缓存，要跨进程，可以再加一层缓存： memcached/xcache/apc/
$g_static_shop = array(); // 变量缓存

// hook model_user_start.php

// ------------> 最原生的 CURD，无关联其他数据。

function shop__create($arr) {
	// hook model_shop__create_start.php
	$r = db_insert('shop', $arr);
	// hook model_shop__create_end.php
	return $r;
}

function shop__update($sid, $update) {
	// hook model_shop__update_start.php
	$r = db_update('shop', array('sid' => $sid), $update);
	// hook model_shop__update_end.php
	return $r;
}

function shop__read($sid) {
	// hook model_shop__read_start.php
	$shop = db_find_one('shop', array('sid' => $sid));
	// hook model_shop__read_end.php
	return $shop;
}

function shop__read_by_host($shophost){
	$shop = db_find_one('shop', array('shophost' => $shophost));
	// hook model_shop__read_end.php
	return $shop;
}

function shop__delete($sid) {
	// hook model_shop__delete_start.php
	$r = db_delete('shop', array('sid' => $sid));
	// hook model_shop__delete_end.php
	return $r;
}

function shop__find($cond = array(), $orderby = array(), $page = 1, $pagesize = 1000) {
	// hook model_shop__find_start.php
	$shoplist = db_find('shop', $cond, $orderby, $page, $pagesize, 'sid');
	// hook model_shop__find_end.php
	return $shoplist;
}
// ------------> 关联 CURD，主要是强相关的数据，比如缓存。弱相关的大量数据需要另外处理。

// 新建店铺（未审核）
function shop_create($uid, $name,$logo ,$intro) {
	$r = shop__create(array(
		'uid' => $uid,
		'name' => $name,
		'logo' => $logo,
		'intro' => $intro,
		'review' => 0,
	));
	return $r;
}

// 更新店铺
function shop_update($sid, $update) {
	$r = shop__update($sid, $update);

	return $r;
}

// 查看单个店铺
function shop_read($sid) {
	$shop = shop__read($sid);
	return $shop;
}

function shop_read_cache($sid) {
	global $conf;
	$shop = cache_get('shop-' . $sid);

	// hook model_shop_list_cache_start.php

	if ($shop === NULL) {
		$shop = shop_read($sid);
		cache_set('shop-' . $sid, $shop, 60);
	}
	// hook model_shop_list_cache_end.php
	return $shop;
};

// 查看所有已审核店铺
function shop_read_all($review = 1) {
	$cond = array();
	$cond['review'] = $review;
	$shoplist = shop__find($cond);
	return $shoplist;
}

function shop_read_uid($uid) {
	$cond = array();
	$cond['uid'] = $uid;
	$shoplist = db_find('shop', $cond);
	return $shoplist;
}

// 店铺审核
function shop_review($sid, $review) {
	$update = array();
	$update['review'] = $review;
	$r = shop_update($sid, $update);
	return $r;
}

function shop_maxid() {
	// hook model_shop_maxid_start.php
	$n = db_maxid('shop', 'sid');
	// hook model_shop_maxid_end.php
	return $n;
}

?>